package com.csi.controller.back;

import cn.hutool.jwt.JWTUtil;
import com.csi.domain.*;
import com.csi.service.RoleService;
import com.csi.utils.ReturnResults;
import jakarta.servlet.http.HttpServletRequest;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.net.URLDecoder;
import java.nio.charset.StandardCharsets;
import java.util.List;

/**
 * 角色管理
 */
@RequestMapping("/role")
@RestController
public class RoleController {
    @Autowired
    private RoleService roleService;

    /**
     * 新增角色
     * @param roleName 角色名称
     * @param request 请求对象
     * @return
     */
    @PostMapping(value = "save")
    public Object save(@RequestBody Role role,HttpServletRequest request){
        String roleName = role.getRoleName();
        long hotelId = Long.parseLong(JWTUtil.parseToken(request.getHeader("token")).getPayload("hotelId").toString());
        role.setHotelId(hotelId);
        int  saved = roleService.save(role);
        if (saved > 0){
            return new Result("新增角色成功！", ReturnResults.ACCESS_SUCCESS);
        }
        return new Result("新增员工失败！", ReturnResults.ACCESS_FAILED);
    }


    /**
     * 根据角色名查找角色
     * @param roleName 角色名
     * @return
     */
    @GetMapping(value = "findByName")
    public Object findByName(@RequestParam("roleName") String roleName,HttpServletRequest request){
//        String roleName = URLDecoder.decode(role.getRoleName(), StandardCharsets.UTF_8);
        long hotelId = Long.parseLong(JWTUtil.parseToken(request.getHeader("token")).getPayload("hotelId").toString());
        Role role = roleService.findByName(roleName,hotelId);
        if(role != null){
            return new Result<Role>("查找成功", ReturnResults.ACCESS_SUCCESS,role);
        }
        return new Result("查找失败！", ReturnResults.ACCESS_FAILED);
    }

    /**
     * 查找角色
     * @return List<Role> 集合
     */
    @GetMapping(value = "findRole")
    public Object findRole(HttpServletRequest request){
        String token = request.getHeader("token");
        long hotelId = Long.parseLong(JWTUtil.parseToken(token).getPayload("hotelId").toString());
        List<Role> roleList = roleService.findRole(hotelId);
        if(roleList != null){
            return new Result<List<Role>>("查找成功", ReturnResults.ACCESS_SUCCESS,roleList);
        }
        return new Result("查找失败！", ReturnResults.ACCESS_FAILED);
    }



    /**
     * 查找所有角色权限
     * @return List<Power>
     */

    @GetMapping(value = "findAllPower")
    public Object findAllPower() {
        List<Power> powerList = roleService.findAllPower();
        if (powerList != null){
            return new Result<List<Power>>("查找成功", ReturnResults.ACCESS_SUCCESS,powerList);
        }
        return new Result("查找失败！", ReturnResults.ACCESS_FAILED);
    }
    /**
     * 通过角色id删除角色
     * @param roleId 角色id
     * @return 条数
     */
    @GetMapping(value = "deleteRole")
    public  Object deleteRolePowerById(@RequestParam("id")long roleId){
        int deleted = roleService.deleteRoleById(roleId);
        if (deleted >0){
            return new Result("删除角色成功！", ReturnResults.ACCESS_SUCCESS);
        }
        return new Result("删除角色失败！", ReturnResults.ACCESS_FAILED);
    }
    /**
     * 更新用户权限
     * @param role 用户权限
     * @return 条数
     */@PostMapping(value = "updateRolePower")
    public Object updateRolePower(@RequestBody Role role){
        int updated = roleService.updateRolePower(role);
        if (updated >0){
            System.out.println(updated);
            return new Result("更改角色权限成功！", ReturnResults.ACCESS_SUCCESS);
        }
        return new Result("更改角色权限失败！", ReturnResults.ACCESS_FAILED);
    }

    /**
     * 根据角色id查找角色
     * @param roleId 角色id
     * @return
     */
    @GetMapping(value = "findRoleById")
    public Object findRoleById(@RequestParam("id") long roleId){
        Role role = roleService.findRoleById(roleId);
        List<Power> powerList = roleService.findPowerById(roleId);
        role.setPowerList(powerList);
        if(role != null){
            return new Result<Role>("查找成功", ReturnResults.ACCESS_SUCCESS,role);
        }
        return new Result("查找失败！", ReturnResults.ACCESS_FAILED);
    }

    /**
     * 通过角色id查找角色权限
     * @return List<Power>
     */
    @GetMapping(value = "findPowerById")
    public Object findPowerById(@RequestParam("roleId") long roleId){
        List<Power> powerList = roleService.findPowerById(roleId);
        if (powerList != null){
            return new Result<List<Power>>("查找成功", ReturnResults.ACCESS_SUCCESS,powerList);
        }
        return new Result("查找失败！", ReturnResults.ACCESS_FAILED);
    }

    /**
     * 更新角色名称
     * @param roleName
     * @param request
     * @return
     */
    @PostMapping(value = "updateRole")
    public Object updateRoleName(@RequestBody Role role){
        int updated = roleService.updateRole(role);
        if (updated >0){
            return new Result("更改角色名称成功！", ReturnResults.ACCESS_SUCCESS);
        }
        return new Result("更改角色名称失败！", ReturnResults.ACCESS_FAILED);
    }

    /**
     * 是否为初始管理员
     * @param roleId
     * @return
     */
    @GetMapping("IfInitialAdmin")
    public Object IfInitialAdmin(@RequestParam("id") long roleId,HttpServletRequest request){
        String token = request.getHeader("token");
        long hotelId = Long.parseLong(JWTUtil.parseToken(token).getPayload("hotelId").toString());
        boolean isInitialAdmin = roleService.isInitialAdmin(roleId,hotelId);
        if (isInitialAdmin){
            return new Result("该角色是初始管理员", ReturnResults.ACCESS_SUCCESS,isInitialAdmin);
        }
        return new Result("该角色不是初始管理员", ReturnResults.ACCESS_FAILED,isInitialAdmin);
    }
}
